<?php
namespace app\index\controller;

use Workerman\Lib\Timer;
use think\Db;
class Index
{
    public function index()
    {
       die();
      return view();
    }

    public function jihua(){
        $bus = (new \app\common\model\Bus())->where(['state' => 0])->select();
        $data = [];
        foreach ($bus as $key => $value) {
            $bus_order = (new \app\common\model\DriverQOrder())
                  ->where(['agent_id' => $value['agent_id'],'batch' => date("Ymd"),'bus_id' => $value['id'],'id' => 1])
                  ->find();
            if(!$bus_order){
              $order_on = 'Q' . date('YmdHis') . rand(1000, 9999);
              $goods_num = (new \app\common\model\UserOrders())
                  ->alias("user_order")
                  ->where([
                    'user_order.agent_id' => $value['agent_id'],
                    'user_order.bus_id' => $value['id'],
                    'user_order.state' => 1,
                    'user_order.driver_q_id' => 0,
                    'user_order.q_order_id' => 0
                    ])
                  ->join("user_orders_goods orders_goods","orders_goods.order_id = user_order.id")
                  ->sum('orders_goods.num');
                
              if($goods_num == 0){
                 continue;
              }
              $driver = (new \app\common\model\Driver())->where(['q_bus_id' => $value['id']])->find();
            
              if(empty($driver)){
                continue;
              }
              Db::startTrans();
              try{
                  $message = "订单创建成功,请重试";
                  $process = true;
                 
                  $row = (new \app\common\model\DriverQOrder())->insertGetId([
                    'order_on' => $order_on,
                    'goods_num' => $goods_num,
                    'state' => 1,
                    'start_address' => '',
                    'end_address' =>  $value['address'],
                    'agent_id' => $value['agent_id'],
                    'addtime' => time(),
                    'batch' => date("Ymd"),
                    'driver_id' => $driver['id'],
                    'bus_id' => $value['id'],
                    'end_latitude' => $value['latitude'],
                    'end_longitude' => $value['longitude'],
                  ]);
                
                  if(!$row){
                    $process = false;
                    $message = "订单创建失败,添加记录失败";
                  }
                  $row = (new \app\common\model\UserOrders())
                      ->where([
                        'bus_id' => $value['id'],'batch' => date("Ymd"),
                        'state' => 1,
                        'driver_q_state' => 0
                        ])
                      ->update([
                        'q_order_id' => $row,
                        'driver_q_id' => $driver['id'],
                        'driver_q_state' => 1,
                      ]);
                  if(!$row){
                    $process = false;
                    $message = "订单创建失败,修改订单信息失败";
                  }
                  if ($process !== false) {
                      Db::commit(); // 提交事务
                  } else {
                      Db::rollback(); // 回滚事务
                  }
                }catch (\Exception $e) {
                  $process = false;
                  $message = $e->getMessage();
                  Db::rollback(); // 回滚事务
              }
             
              if ($process !== false) {
                    $data[$key] = ['code' => 0, 'msg' => $value['id'].$value['nickname'].$message,'data' => []];
              }else{
                    $data[$key] = ['code' => 1, 'msg' => $value['id'].'----'.$value['nickname'].'----'.$message,'data' => []];
              }
            }
        }
        var_dump($data);die;
    }
    public function jihua_s(){
      die('111');
      $bus = (new \app\common\model\Bus())->where(['state' => 0])->select();
      $data = [];
      foreach ($bus as $key => $value) {
          $bus_order = (new \app\common\model\DriverSOrder())
                ->where(['agent_id' => $value['agent_id'],'batch' => date("Ymd"),'bus_id' => $value['id']])
                ->find();
          if(!$bus_order){
            $order_on = 'S' . date('YmdHis') . rand(1000, 9999);
            $goods_num = (new \app\common\model\UserOrders())
                ->alias("user_order")
                ->where([
                  'user_order.agent_id' => $value['agent_id'],
                  'user_order.bus_id' => $value['id'],
                  // 'user_order.state' => 1,
                  // 'user_order.driver_id' => 0,
                  // 'user_order.s_order_id' => 0
                  ])
                ->join("user_orders_goods orders_goods","orders_goods.order_id = user_order.id")
                ->sum('orders_goods.num');
           
            if($goods_num == 0){
               continue;
            }
            $driver = (new \app\common\model\Driver())->where(['s_bus_id' => $value['id']])->find();
            if(empty($driver)){
              continue;
            }
            Db::startTrans();
            try{
                $message = "订单创建成功,请重试";
                $process = true;
               
                $row = (new \app\common\model\DriverSOrder())->insertGetId([
                  'order_on' => $order_on,
                  'goods_num' => $goods_num,
                  'state' => 0,
                  'start_address' => '',
                  'end_address' =>  $value['address'],
                  'agent_id' => $value['agent_id'],
                  'addtime' => time(),
                  'batch' => date("Ymd"),
                  'driver_id' => $driver['id'],
                  'bus_id' => $value['id'],
                  'end_latitude' => $value['latitude'],
                  'end_longitude' => $value['longitude'],
                ]);
              
                if(!$row){
                  $process = false;
                  $message = "订单创建失败,添加记录失败";
                }
                $row = (new \app\common\model\UserOrders())
                    ->where(['bus_id' => $value['id'],'batch' => date("Ymd")])
                    ->update([
                      'q_order_id' => $row,
                      'driver_id' => $driver['id'],
                      'driver_state' => 0,
                    ]);
                if(!$row){
                  $process = false;
                  $message = "订单创建失败,修改订单信息失败";
                }
                if ($process !== false) {
                    Db::commit(); // 提交事务
                } else {
                    Db::rollback(); // 回滚事务
                }
              }catch (\Exception $e) {
                $process = false;
                $message = $e->getMessage();
                Db::rollback(); // 回滚事务
            }
           
            if ($process !== false) {
                  $data[$key] = ['code' => 0, 'msg' => $value['id'].$value['nickname'].$message,'data' => []];
            }else{
                  $data[$key] = ['code' => 1, 'msg' => $value['id'].'----'.$value['nickname'].'----'.$message,'data' => []];
            }
          }
      }
      var_dump($data);die;
  }
}
